/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.rockville.filesystem;

import java.io.File;
import org.apache.log4j.Logger;

/**
 *
 * @author Usman
 */
public class FileUtils {


    private static final Logger log=Logger.getLogger(FileUtils.class);
    /**
     * <pre>
     * Checks if the file exists or not.
     * </pre>
     * @param filePath
     * @return
     */
    public static boolean exists(String filePath) {
    	File myFile = null;
        try {
            myFile = new File(filePath);
            return myFile.exists();            
        } catch(Exception ex) {        	
        } finally {
        	myFile = null;
        }        
        return false;
    }
    
    /**
     * <pre>
     * Converts the given file path to Operation System dependent file path.
     * </pre>
     * @param filePath
     * @return
     */
    public static String osDependentFilePath(String filePath) {
        if(com.rockville.filesystem.RunTime.isLinuxPlatform()) {
            filePath = filePath.replaceAll("\\\\", "/");
            filePath = filePath.replaceAll("//", "/");
            
            if(!filePath.endsWith("/")) 
                filePath = filePath +"/";
        }
        else if(com.rockville.filesystem.RunTime.isWindowsPlatform()) {
            filePath = filePath.replaceAll("//", "/");
            filePath = filePath.replaceAll("/", "\\\\");
            
            if(!filePath.endsWith("\\"))
                filePath = filePath +"\\";
        }
        
        return filePath;
    }
    
    /**
     * 
     * @param filename
     * @return
     */
    public static long getFileSize(String filename) {
        File file = new File(filename);
        return getFileSize(file);
    }
    
    /**
     * 
     * @param file
     * @return
     */
    public static long getFileSize(File file) {

        if (!file.exists() || !file.isFile()) {
            log.debug("File doesn\'t exist");
            return -1;
        }

        //Here we get the actual size
        return file.length();
    }
    
}
